package com.atguigu.gmall.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.BaseManageService;
import com.atguigu.gmall.product.service.BaseSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,rollbackFor = Exception.class)
public class BaseManageServiceImpl implements BaseManageService {

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;

    @Autowired
    private SkuInfoMapper skuInfoMapper;

    @Autowired
    private SkuImageMapper skuImageMapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;



    @Override
    public List<BaseCategory1> getBaseCategory1List() {
        List<BaseCategory1> baseCategory1s = baseCategory1Mapper.selectList(null);
        return baseCategory1s;
    }

    @Override
    public List<BaseCategory2> getCategory2List(Long category1Id) {
        QueryWrapper<BaseCategory2> baseCategory2QueryWrapper = new QueryWrapper<>();
        baseCategory2QueryWrapper.eq("category1_id", category1Id);
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(baseCategory2QueryWrapper);
        return baseCategory2List;
    }

    @Override
    public List<BaseCategory3> getCategory3List(Long category2Id) {
        QueryWrapper<BaseCategory3> baseCategory3QueryWrapper = new QueryWrapper<>();
        baseCategory3QueryWrapper.eq("category2_id", category2Id);
        List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(baseCategory3QueryWrapper);
        return baseCategory3List;
    }

    @Override
    public List<BaseAttrInfo> getBaseAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoMapper.selectBaseAttrInfoList(category1Id, category2Id, category3Id);
        return baseAttrInfoList;
    }

    @Override
    @Transactional(readOnly = false)
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        Long attrInfoId = baseAttrInfo.getId();
        if (attrInfoId == null) {
            //新增平台属性
            baseAttrInfoMapper.insert(baseAttrInfo);
            attrInfoId = baseAttrInfo.getId();

        }else{
            //修改平台属性
            baseAttrInfoMapper.updateById(baseAttrInfo);
            baseAttrValueMapper.delete(new QueryWrapper<BaseAttrValue>().eq("attr_id", attrInfoId));
        }
        baseAttrValueMapper.batchInsert(attrInfoId, baseAttrInfo.getAttrValueList());
    }

    @Override
    public List<BaseAttrValue> getBaseAttrValueList(Long attrId) {
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);

        if (baseAttrInfo == null) {
            return null;
        }

        List<BaseAttrValue> attrValueList = baseAttrValueMapper.selectList(new QueryWrapper<BaseAttrValue>().eq("attr_id", attrId));
        return attrValueList;

    }

    @Override
    public List<BaseSaleAttr> getBaseSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttrs = baseSaleAttrMapper.selectList(null);
        return baseSaleAttrs;
    }


    @Override
    public BaseCategoryView getCategoryViewByCategory3Id(Long category3Id) {
        BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
        return baseCategoryView;
    }

    @Override
    @GmallCache(prefix = "category")
    public List<JSONObject> getBaseCategoryList() {
        int index=0;

        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);

        Map<Long, List<BaseCategoryView>> base1 = baseCategoryViews.stream()
                .collect(Collectors.groupingBy(baseCategoryView -> {
                    return baseCategoryView.getCategory1Id();
                }));
        List<JSONObject> cateAll = new ArrayList<>();
        Set<Map.Entry<Long, List<BaseCategoryView>>> base1Entries = base1.entrySet();
        for (Map.Entry<Long, List<BaseCategoryView>> base1Entrie : base1Entries) {
            index++;
            Long base1Key = base1Entrie.getKey();
            List<BaseCategoryView> base1Value = base1Entrie.getValue();
            BaseCategoryView categoryView = base1Value.get(0);
            String category1Name = categoryView.getCategory1Name();

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("index",index);
            jsonObject1.put("categoryId",base1Key);
            jsonObject1.put("categoryName",category1Name);

            List<JSONObject> cateAll2 = new ArrayList<>();

            Map<Long, List<BaseCategoryView>> collect2 = base1Value.stream()
                    .collect(Collectors.groupingBy(BaseCategoryView -> {
                        return BaseCategoryView.getCategory2Id();
                    }));
            Set<Map.Entry<Long, List<BaseCategoryView>>> entries2 = collect2.entrySet();
            for (Map.Entry<Long, List<BaseCategoryView>> base2Entrie : entries2) {
                Long base2Key = base2Entrie.getKey();
                List<BaseCategoryView> base2Value = base2Entrie.getValue();

                BaseCategoryView categoryView2 = base2Value.get(0);
                String category2Name = categoryView2.getCategory2Name();

                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("categoryId", base2Key);
                jsonObject2.put("categoryName", category2Name);

                Map<Long, List<BaseCategoryView>> collect3 = base2Value.stream()
                        .collect(Collectors.groupingBy(BaseCategoryView -> {
                            return BaseCategoryView.getCategory3Id();
                        }));
                Set<Map.Entry<Long, List<BaseCategoryView>>> entries3 = collect3.entrySet();

                List<JSONObject> cateall3 = new ArrayList<>();
                for (Map.Entry<Long, List<BaseCategoryView>> base3Entrie : entries3) {
                    Long base3Key = base3Entrie.getKey();
                    List<BaseCategoryView> base3Value = base3Entrie.getValue();

                    BaseCategoryView categoryView3 = base3Value.get(0);
                    String category3Name = categoryView3.getCategory3Name();

                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("categoryId",base3Key);
                    jsonObject3.put("categoryName",category3Name);

                    cateall3.add(jsonObject3);

                }

                jsonObject2.put("categoryChild", cateall3);
                cateAll2.add(jsonObject2);
            }

            jsonObject1.put("categoryChild", cateAll2);
            cateAll.add(jsonObject1);
        }

        return cateAll;
    }
}
